package com.hongyue.a21dian;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class a21dian {
    public static void main(String[] args) {
        ArrayList<String> arr = new ArrayList<>();
        FaPai(arr);
        System.out.println("欢迎游玩21点");
        System.out.println("你已获得100筹码");
        int chouMa = 100;
        int i = 0;
        Scanner sc = new Scanner(System.in);
        a:
        while (chouMa >= 0) {
            if (i >= 30) {
                i = 0;
                FaPai(arr);
            }
            System.out.println("开始发牌");
            System.out.println("您现在拥有"+chouMa+"筹码 请下注");
            int xz = sc.nextInt();
            System.out.println("您已下注"+xz+"筹码");
            chouMa -= xz;
            ArrayList<String> wanJia = new ArrayList<>();
            ArrayList<String> dianNao = new ArrayList<>();
            for (int j = 0; j < 4; j++) {
                if (i % 2 == 0) {
                    wanJia.add(arr.get(i));
                    System.out.println("您拿到了"+arr.get(i));
                    i++;
                } else {
                    dianNao.add(arr.get(i));
                    i++;
                }
            }
            int dianShu;
            while (true) {
                dianShu = JiSuan(wanJia);
                System.out.println("您现在的手牌为" + wanJia + "点数为" + dianShu);
                if (dianShu > 21) {
                    System.out.println("您的点数已经超过21点");
                    System.out.println("很遗憾 您输掉了这一局");
                    continue a;
                } else if (dianShu == 21) {
                    System.out.println("您的点数为21点");
                    System.out.println("您获得胜利并赢得双倍筹码");
                    chouMa += xz * 3;
                    continue a;
                }
                System.out.println("请问是否要牌 1要牌 2开牌");
                int zl = sc.nextInt();
                if (zl == 1) {
                    wanJia.add(arr.get(i));
                    System.out.println("您拿到了" + arr.get(i));
                    i++;
                } else {
                    break;
                }
            }
            int ds = JiSuan(dianNao);
            while (ds <= 16) {
                dianNao.add(arr.get(i));
                ds = JiSuan(dianNao);
                i++;
            }
            if (ds > 21) {
                System.out.println("您的对手点数超过21点");
                System.out.println("您获得胜利赢得"+xz+"筹码");
                chouMa += xz*2;
            }
            System.out.println("您的对手点数为"+ds);
            if (dianShu > ds) {
                System.out.println("您的点数大于对手的点数");
                System.out.println("您获得胜利赢得"+xz+"筹码");
                chouMa += xz*2;
            } else if (dianShu < ds) {
                System.out.println("您的点数小于于对手的点数");
                System.out.println("很遗憾您输掉了");
            } else {
                System.out.println("您和对手的点数相同");
                System.out.println("平局计算");
                chouMa += xz;
            }
        }
    }

    private static int JiSuan(ArrayList<String> pai) {
        String a = "A234567891JQK";
        int max = 0;
        for (int i = 0; i < pai.size(); i++) {
            String s = pai.get(i);
            char c = s.charAt(1);
            int i2 = a.indexOf(c);
            int i1 = i2 > 9 ? (i2 - 10) : i2;
            max += i1 + 1;
        }
        return max;
    }

    private static void FaPai(ArrayList<String> arr) {
        String[] hua = {"♥","♠","♣","♦"};
        String[] zi = {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2"};
        for (int i = 0; i < hua.length; i++) {
            for (int j = 0; j < zi.length; j++) {
                arr.add(hua[i] + zi[j]);
            }
        }
        Collections.shuffle(arr);
    }
}
